package se581.banker.event;

import se581.banker.BankerConstants.TrackerEventGroup;
import se581.banker.BankerConstants.TrackerEventType;

/** 
 * Singleton to create TrackerEvent objects out of the input file lines
 * 
 * @author mmichalak
 *
 */
public class EventFactory {
	
	private EventFactory() { super(); }
	
	public static TrackerEvent createEvent(String line) {
		String[] tokens = line.split(" ");
		
		TrackerEvent ev = new TrackerEvent();

		ev.setTick(Integer.parseInt(tokens[0]));
		
		if (tokens[1].equalsIgnoreCase("QUERY") && tokens[2].equalsIgnoreCase("WHEREIS")) {
			ev.setEventType(TrackerEventType.WHEREIS);
			ev.setId(tokens[3]);
			ev.setEventGroup(TrackerEventGroup.QUERY);
		}
		else if (tokens[1].equalsIgnoreCase("QUERY") && tokens[2].equalsIgnoreCase("LASTUPDATE")) {
			ev.setEventType(TrackerEventType.LASTUPDATE);
			ev.setId(tokens[3]);
			ev.setEventGroup(TrackerEventGroup.QUERY);
		}
		else if (tokens[1].equalsIgnoreCase("QUERY") && tokens[2].equalsIgnoreCase("HISTORY")) {
			ev.setEventType(TrackerEventType.HISTORY);
			ev.setId(tokens[3]);
			ev.setEventGroup(TrackerEventGroup.QUERY);
		}
		else if (tokens[1].equalsIgnoreCase("QUERY") && tokens[2].equalsIgnoreCase("BALANCE")) {
			ev.setEventType(TrackerEventType.BALANCE);
			ev.setId(tokens[3]);
			ev.setEventGroup(TrackerEventGroup.QUERY);
		}
		else if (tokens[1].equalsIgnoreCase("BLANK")) {
			ev.setEventGroup(TrackerEventGroup.BLANK);
			ev.setEventType(TrackerEventType.BLANK);
		}
		else {  // UPDATE EVENT
			if (tokens[1].equalsIgnoreCase("NEW")) ev.setEventType(TrackerEventType.NEW);
			else if (tokens[1].equalsIgnoreCase("MOVE")) ev.setEventType(TrackerEventType.MOVE);
			else if (tokens[1].equalsIgnoreCase("UPDATE")) ev.setEventType(TrackerEventType.UPDATE);
			else if (tokens[1].equalsIgnoreCase("END")) ev.setEventType(TrackerEventType.END);
			
			ev.setEventGroup(TrackerEventGroup.UPDATE);
			ev.setId(tokens[2]);
			ev.setTransactionAmt(Integer.parseInt(tokens[3]));
//			ev.setYCoord(Integer.parseInt(tokens[4]));  REMOVE pOSITION LONG
		}
		
		return ev;
	}
}
